package com.whsxt.controller;

import com.whsxt.domain.Basket;
import com.whsxt.service.BasketService;
import com.whsxt.vo.CartMoney;
import com.whsxt.vo.ShopCartResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-03-24 17:03
 * @Description:
 */
@RestController
@RequestMapping
@Api(tags = "购物车接口")
public class CartController {


    @Autowired
    private BasketService basketService;


    @GetMapping("/p/shopCart/prodCount")
    @ApiOperation(value = "查询购物车商品数量")
    public ResponseEntity<Integer> getCartCount() {
        String openId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        Integer count = basketService.findCartCount(openId);
        return ResponseEntity.ok(count);
    }


    @PostMapping("/p/shopCart/changeItem")
    @ApiOperation(value = "修改购物车商品数量")
    public ResponseEntity<Void> changeItem(@RequestBody Basket basket) {
        String openId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        basket.setUserId(openId);
        basketService.changeItem(basket);
        return ResponseEntity.ok().build();
    }


    @PostMapping("/p/shopCart/info")
    @ApiOperation(value = "查询购物车的详情")
    public ResponseEntity<List<ShopCartResult>> getShopCartInfo() {
        String openId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        List<ShopCartResult> shopCartResults = basketService.getShopCartInfo(openId);
        return ResponseEntity.ok(shopCartResults);
    }


    @DeleteMapping("/p/shopCart/deleteItem")
    @ApiOperation(value = "删除购物车")
    public ResponseEntity<Void> deleteBasket(@RequestBody List<Long> basketIds) {
        basketService.removeByIds(basketIds);
        return ResponseEntity.ok().build();
    }


    /**
     * 总金额是多少，满减是多少，优惠多少，最终是多少
     *
     * @param basketIds
     * @return
     */
    @PostMapping("/p/shopCart/totalPay")
    @ApiOperation(value = "计算购物车中选中商品的总金额")
    public ResponseEntity<CartMoney> totalPay(@RequestBody List<Long> basketIds) {
        CartMoney cartMoney = basketService.calcMoney(basketIds);
        return ResponseEntity.ok(cartMoney);
    }


    /**
     * 远程调用购物车，拿到购物车的商品集合
     *
     * @param basketIds
     * @return
     */
    @PostMapping("/getBasketByIds")
    public List<Basket> getBasketByIds(@RequestParam List<Long> basketIds) {
        return basketService.listByIds(basketIds);
    }


    /**
     * 清空购物车
     *
     * @param openId
     * @param skuIds
     */
    @PostMapping("/cleanCart")
    public void cleanCart(@RequestParam String openId, @RequestParam List<Long> skuIds) {
        basketService.cleanCart(openId, skuIds);
    }


}
